package uk.ac.aber.paws.client.idlelogger.languages;

import java.awt.CheckboxMenuItem;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import uk.ac.aber.paws.client.idlelogger.IdleLogger;
import uk.ac.aber.paws.client.idlelogger.PAWSDisabler;
import uk.ac.aber.paws.client.idlelogger.gui.PAWSSysTray;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * DisableOptionHandler.java
 * 
 * 
 * a) Operations to process the DBCall queue
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class DisableOptionHandler implements ItemListener {

	private String lang;
	private String optionKey;
	
	public DisableOptionHandler(String optionKey) {
		this.optionKey = optionKey;
	}
	
	public String getLang() {
		return this.lang;
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		
		Menu disableMenu = PAWSSysTray.getDisablePAWS();
		
		for (int i=0;i<disableMenu.getItemCount();i++) {
			
			CheckboxMenuItem mi = (CheckboxMenuItem)disableMenu.getItem(i);
			
			// stop other threads already running
			if (!mi.getName().equals(optionKey) && mi.getState() && IdleLogger.getDisabler() != null) {
				IdleLogger.getDisabler().interrupt();
				mi.setState(false);
			}
			
			// one of the active options is deticked
			else if (mi.getName().equals(optionKey) && !mi.getState() && IdleLogger.getDisabler() != null) {
				IdleLogger.getDisabler().interrupt();
			}
			else if (mi.getName().equals(optionKey) && mi.getState() && IdleLogger.getDisabler() == null) {
				if (!IdleLogger.getDisablePAWS()) {
				
					if (IdleLogger.getDisabler() == null) {
						if (optionKey.equals("DISABLEOPTION1")) {
							
							IdleLogger.setDisabler(new PAWSDisabler(30*60*1000));
						}
						else if (optionKey.equals("DISABLEOPTION2")) {
							IdleLogger.setDisabler(new PAWSDisabler(60*60*1000));
						}
						else if (optionKey.equals("DISABLEOPTION3")) {
							IdleLogger.setDisabler(new PAWSDisabler(120*60*1000));
						}
						else if (optionKey.equals("DISABLEOPTION4")) {
							IdleLogger.setDisabler(new PAWSDisabler(240*60*1000));
						}
						else if (optionKey.equals("DISABLEOPTION5")) {
							IdleLogger.setDisabler(new PAWSDisabler(480*60*1000));
						}
					}
				}
			}
		}
	}
}
